/**
 * autho fengbin
 * 2008-9-10 
 */
package com.wsj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.springframework.dao.DataAccessException;

import com.wsj.dao.IComAdTypeDao;
import com.wsj.dao.IHomeSiteSearchHotKeyDao;
import com.wsj.service.IHomeSiteSearchHotKeyServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.page.PageResultSet;
import com.wsj.web.form.search.HomeSiteSearchHotKeyForm;
import com.wsj.web.vo.homeSite.ComAdType;
import com.wsj.web.vo.homeSite.HomeSiteSearchHotKey;

/**
 * @author fengbin
 *
 */
public class HomeSiteSearchHotKeyServImpl implements IHomeSiteSearchHotKeyServ
{
	  IHomeSiteSearchHotKeyDao homeSiteSearchHotKeyDao; 
	  
	  IComAdTypeDao  comAdTypeDao;

	public IComAdTypeDao getComAdTypeDao() {
		return comAdTypeDao;
	}

	public void setComAdTypeDao(IComAdTypeDao comAdTypeDao) {
		this.comAdTypeDao = comAdTypeDao;
	}

	public IHomeSiteSearchHotKeyDao getHomeSiteSearchHotKeyDao() {
		return homeSiteSearchHotKeyDao;
	}

	public void setHomeSiteSearchHotKeyDao(
			IHomeSiteSearchHotKeyDao homeSiteSearchHotKeyDao) {
		this.homeSiteSearchHotKeyDao = homeSiteSearchHotKeyDao;
	}

	/*****
	 * 获取广告分类列表
	 */
	public List loadComAdTypeList() throws BusinessException
	{
		//加载广告类别类表
		return this.getComAdTypeDao().loadAllComAdTypeList();
	}

	/****
	 * 添加热门关键字
	 */
	public boolean addHomeSiteSearchHotKey(HomeSiteSearchHotKey homeSiteSearchHotKey) throws BusinessException
	{
		try 
		{
			this.getHomeSiteSearchHotKeyDao().addHomeSiteSearchHotKeyDao(homeSiteSearchHotKey);
			return true;
		} 
		catch (Exception e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/****
	 * 通过搜索分类id,加载搜索分类对象
	 */
	public ComAdType loadComAdTypeById(Integer comAdTypeId) throws BusinessException
	{
		return  this.getHomeSiteSearchHotKeyDao().loadComAdTypeByIdDao(comAdTypeId);
	}

	/*****
	 * 检查该分类下面热门关键字是否重复
	 */
	public boolean checkHomeSiteSearchHotKey(Integer id,String keywords, Integer comAdTypeId, String addOrUpdate)throws BusinessException
	{
		List li = this.getHomeSiteSearchHotKeyDao().loadHomeSiteSearchHotKeyByComAdTypeIdAndKeywordsDao(id,keywords,  comAdTypeId,  addOrUpdate);
		if(li==null || li.isEmpty())
		{
			return true;
		}
		return false;
	}


   /*****
    * 通过id,加载热门关键字对象
    */
	public HomeSiteSearchHotKey loadHomeSiteSearchHotKeyById(Integer id)throws BusinessException
	{
	   return this.getHomeSiteSearchHotKeyDao().loadHomeSiteSearchHotKeyByIdDao(id);
	}

	/*****
	 * 通过相关参数,加载热门关键字对象列表
	 */
	public PageResultSet loadHomeSiteSearchHotKeyList(Integer page,Integer pageSize,HomeSiteSearchHotKeyForm homeSiteSearchHotKeyForm)throws BusinessException
	{
		PageResultSet pageResultSet =  this.getHomeSiteSearchHotKeyDao().loadHomeSiteSearchHotKeyListDao( page, pageSize,homeSiteSearchHotKeyForm);
		List li =pageResultSet.getResult();
		List retList=new ArrayList();
		if(pageResultSet!=null && li!=null)
		{
			for(int j=0;j<li.size();j++)
			{
				HomeSiteSearchHotKey homeSiteSearchHotKey = (HomeSiteSearchHotKey)li.get(j);
				Hibernate.initialize(homeSiteSearchHotKey.getComAdType());
				retList.add(homeSiteSearchHotKey);
			}
		}
		pageResultSet.setResult(retList);
		
		return pageResultSet;
		
	}

	
	/*****
	 * 更新热门关键字 
	 */
	public boolean updateHomeSiteSearchHotKey(HomeSiteSearchHotKey homeSiteSearchHotKey)throws BusinessException
	{
		 try {
			this.getHomeSiteSearchHotKeyDao().updateHomeSiteSearchHotKeyDao(homeSiteSearchHotKey);
			return true;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	
	/****
	 * 获取前几个关键字
	 */
	public List loadTopHotKeyList(String typeName, Integer top) throws BusinessException 
	{
		return this.getHomeSiteSearchHotKeyDao().loadTopHotKeyListDao(typeName,  top);
	}
	  
	  

}
